package com.coolgor.xcode;

import java.io.*;
import java.util.List;

public class StackService {

    public static void run(List<String[]> context,String basePath, String basePackage, String moduleName) throws IOException {
        String outputPath = basePath + XcoderConfig.OutputPath + "/service";
        String tableName = null;
        String className = null;
        String keyColumn = null;
        String keyParam = null;
        for (int i = 0; i < context.size(); i++) {
            switch (context.get(i)[0].toLowerCase()) {
                case "table":
                    tableName = context.get(i)[1];
                    className = StringUtil.firstUpper(context.get(i)[2]);
                    break;
                case "key":
                    keyColumn = context.get(i)[1];
                    keyParam = context.get(i)[2];
                    break;
            }
        }

        // File writing ###############
        File file = new File(outputPath);
        if (!file.exists()) file.mkdirs();
        file = new File(outputPath + "/" + className + "Service.java");
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        if (null!=basePackage) {
            if (basePackage.length()>1 && !basePackage.endsWith(".")) {
                basePackage = basePackage + ".";
            } else {
                basePackage = "";
            }
            if (null!=moduleName && !"".equals(moduleName) && !".".equals(moduleName)) {
                bw.write("package "+basePackage+"service."+moduleName+";\n");
                moduleName = moduleName+".";
            } else {
                bw.write("package "+basePackage+"service;\n");
                moduleName = "";
            }
            bw.newLine();
            bw.write("import "+basePackage+"entity."+moduleName+className+";\n");
            bw.write("import "+basePackage+"dto."+moduleName+className+"Execution;\n");
            bw.write("import "+basePackage+"exceptions."+moduleName+className+"OperationException;\n");
        }
        bw.newLine();
        bw.write("import java.util.List;\n");
        bw.newLine();
        bw.newLine();
        bw.write("public interface "+className+"Service {\n");
        bw.newLine();
        bw.write("\t"+className+"Execution getByID(Long "+keyParam+");\n");
        bw.newLine();
        bw.write("\t"+className+"Execution getList("+className+" "+StringUtil.firstLower(className)+"Condition, int pageIndex, int pageSize);\n");
        bw.newLine();
        bw.write("\t"+className+"Execution add("+className+" "+StringUtil.firstLower(className)+") throws "+className+"OperationException;\n");
        bw.newLine();
        bw.write("\t"+className+"Execution modify("+className+" "+StringUtil.firstLower(className)+") throws "+className+"OperationException;\n");
        bw.newLine();
        bw.write("\t"+className+"Execution remove(Long "+keyParam+") throws "+className+"OperationException;\n");
        bw.newLine();
        bw.write("\t"+className+"Execution remove("+className+" "+StringUtil.firstLower(className)+") throws "+className+"OperationException;\n");
        bw.newLine();
        bw.write("\t"+className+"Execution removeBatchByID(List<Long> recordIDList) throws "+className+"OperationException;\n");
        bw.newLine();
        bw.write("\t"+className+"Execution removeBatch(List<"+className+"> "+StringUtil.firstLower(className)+"List) throws "+className+"OperationException;\n");
        bw.newLine();
        bw.write("}\n");
        bw.close();
    }
}
